﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class FileUtil
{
    /// <summary>
    /// 检查文件是否存在
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static bool IsExists(string path)
    {
        FileInfo file= new FileInfo(path);
        return file.Exists;
    }

    //写入文件
    public static void WriteFile(string path,byte[] data)
    { 
        //获取标准路径
        path = PathUtil.GetStandardPath(path);//先调整正反杠
        //文件夹的路径   最后一个斜杠前面一节为文件夹
        string dir = path.Substring(0, path.LastIndexOf("/"));
        if(!Directory.Exists(dir))//不存在创建
        {//创建一个没有ab后缀的文件加
            Directory.CreateDirectory(dir);
        }
        FileInfo file=new FileInfo(path);
        if(file.Exists)
        {
            file.Delete();
        }
        try
        {
            using (FileStream fs=new FileStream(path,FileMode.Create,FileAccess.Write))
            {
                fs.Write(data, 0, data.Length);
                fs.Close();
            }
        }
        catch(IOException e)
        {
            Debug.LogError(e.Message);
        }
    }
}

